package com.yocard.yxyd.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.MainContract;
import com.yocard.yxyd.ui.fragment.CustomerFragment;
import com.yocard.yxyd.ui.fragment.OrderFragment;
import com.yocard.yxyd.ui.fragment.base.BaseBackFragment;
import com.yocard.yxyd.ui.presenter.MainPresenter;
import com.yocard.yxyd.ui.view.MyFragmentTabHost;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.MainTab;
import com.yocard.yxyd.utils.util.CacheDoubleStaticUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

import static com.yocard.yxyd.event.EventCode.REFRESH_ORDER_LIST;
import static com.yocard.yxyd.event.EventCode.REFRESH_RECEIVED_ORDER_lIST;
import static com.yocard.yxyd.event.EventCode.REFRESH_USERI_NFO;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View, BaseBackFragment.BackHandleInterface {

    @BindView(R.id.tab_content)
    FrameLayout tabContent;
    @BindView(R.id.tab_host)
    MyFragmentTabHost mTabHost;
    protected boolean isScrollTab1 = true;
    protected boolean isScrollTab2 = false;
    private BaseBackFragment fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
        JPushInterface.resumePush(this);     		// 暂停 JPush
//        if (ImmersionBar.isSupportStatusBarDarkFont()) {
//            ImmersionBar.with(this)
//                    .fitsSystemWindows(true)
//                    .statusBarColorInt(Color.BLACK)
//                    .navigationBarColorInt(Color.BLACK)
//                    .autoDarkModeEnable(true, 0.2f)
//                    .init();
//        } else {
//            ImmersionBar.with(this)
//                    .statusBarDarkFont(true, 0.2f)
//                    .navigationBarDarkIcon(true, 0.2f)
//                    .init();
//        }

//        ImmersionBar.with(this).statusBarColor(R.color.transparent)
//                .statusBarDarkFont(true).init();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Boolean isCheckUpdate = (Boolean) CacheDoubleStaticUtils.getSerializable(Constants.KEY_UPDATE, false);
        if (isCheckUpdate) {
            mPresenter.getUpdateInfo();
        }
    }

    @Override
    protected void initEventAndData() {
        Boolean isCheckUpdate = (Boolean) CacheDoubleStaticUtils.getSerializable(Constants.KEY_UPDATE, false);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tab_content);
        mTabHost.getTabWidget().setDividerDrawable(null);
        initTabs();
        if (!isCheckUpdate) {
            mPresenter.getUpdateInfo();
        }
        refreshFromNotify(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        refreshFromNotify(intent);
    }

    private void refreshFromNotify(Intent intent) {
        if(intent.hasExtra("ft") && intent.getStringExtra("ft").equals("order")) {
            setSelect(mTabHost.getTabWidget().getChildTabViewAt(0));
        }
    }

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        for (int i = 0; i < tabs.length; i++) {
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = mTabHost.newTabSpec(mainTab.getResName());
            View indicator = View.inflate(this, R.layout.tab_indicator, null);
            TextView title = indicator.findViewById(R.id.tab_title);
            ImageView icon = indicator.findViewById(R.id.iv_icon);
            Drawable drawable = this.getResources().getDrawable(mainTab.getResIcon());
            icon.setImageDrawable(drawable);
            title.setText(mainTab.getResName());
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            mTabHost.addTab(tab, mainTab.getClz(), null);

            mTabHost.getTabWidget().getChildTabViewAt(i).setTag(i);
            mTabHost.getTabWidget().getChildTabViewAt(i).setOnClickListener(clickListener);
        }
    }

    @Override
    public void onBackPressed() {
        if(fragment != null && fragment.onBackPressed()){
            return;
        }
        CommonUtils.doubleClickExitApp();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setSelect(v);
        }
    };

    private void setSelect(View v) {
        int id = (int) v.getTag();
        mTabHost.setCurrentTab(id);

        String currentTabTag = mTabHost.getCurrentTabTag();
        Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(currentTabTag);
        if (id == 0) {
            fragment = (BaseBackFragment) fragmentByTag;
            EventBus.getDefault().post(new CommonBean(REFRESH_ORDER_LIST));
            if (isScrollTab1) {
                if (fragmentByTag instanceof OrderFragment) {
                    OrderFragment orderFragment = (OrderFragment) fragmentByTag;
                    orderFragment.scrollCurrentFragmentTop();
                }
            }
        }

        if (id == 1) {
            fragment = (BaseBackFragment) fragmentByTag;
            EventBus.getDefault().post(new CommonBean(REFRESH_RECEIVED_ORDER_lIST));
            if (isScrollTab2) {
                if (fragmentByTag instanceof CustomerFragment) {
                    CustomerFragment customerFragment = (CustomerFragment) fragmentByTag;
                    customerFragment.smoothScrollToTop();
                }
            }
        }

        if(id == 2) {
            fragment = null;
            EventBus.getDefault().post(new CommonBean(REFRESH_USERI_NFO));
        }

        if (id == 0) {
            MobclickAgent.onEvent(mContext, "2", "click_qiangdan_root");
            isScrollTab1 = true;
        } else {
            isScrollTab1 = false;
        }

        if (id == 1) {
            isScrollTab2 = true;
        } else {
            isScrollTab2 = false;
        }
    }

    @Override
    public void onSelectedFragment(BaseBackFragment fragment) {
        this.fragment = fragment;
    }
}
